package com.wolfpire.subject.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.wolfpire.profession.service.ProfessionService;
import com.wolfpire.subject.service.SubjectService;
import com.wolfpire.system.common.AuthorityUtils;
import com.wolfpire.system.common.EasyuiPage;
import com.wolfpire.system.common.EasyuiPagination;
import com.wolfpire.system.common.Page;
import com.wolfpire.system.common.Result;
import com.wolfpire.system.common.springmvc.PrincipalArgument;
import com.wolfpire.system.common.util.StringUtils;
import com.wolfpire.system.model.Authority;
import com.wolfpire.system.model.DataDict;
import com.wolfpire.system.model.Org;
import com.wolfpire.system.model.Profession;
import com.wolfpire.system.model.Role;
import com.wolfpire.system.model.Subject;
import com.wolfpire.system.model.User;
import com.wolfpire.system.service.RoleService;

/**
 * @author xp
 *
 */

@Controller
@RequestMapping("/subject")
public class SubjectController {
	
	@Resource private SubjectService subjectService;
	
	@RequestMapping("/list")
	public ModelAndView list(@PrincipalArgument User user, @RequestParam Long menuId) {
		//获取对应菜单的操作权限
//		List<Authority> operAuthorities = getAperAuthorities(user, menuId);
		List<Authority> operAuthorities = AuthorityUtils.getAperAuthorities(user, menuId);
		return new ModelAndView("subjectManage/subject_list", "operAuthorities", operAuthorities);
	}
	
	@ResponseBody
	@RequestMapping("/queryList")
	public EasyuiPagination<Subject> queryList(EasyuiPage<Subject> page, Subject filterSubject) {
		Page<Subject> subjects = this.subjectService.setPageDataList(page, filterSubject);
		return new EasyuiPagination<Subject>(subjects);
	}
	
	@ResponseBody
	@RequestMapping("/queryList1")
	public EasyuiPagination<Map<String, Object>> queryList1(EasyuiPage<Map<String, Object>> page, Subject filterSubject
			,@RequestParam(value = "usergroupId", required = false) long usergroupId) {
		Page<Map<String, Object>> subjects = this.subjectService.findSubjects(page, filterSubject, usergroupId);
		//Page<Subject> subjects = this.subjectService.setPageDataList(page, filterSubject);
		return new EasyuiPagination<Map<String, Object>>(subjects);
		//return null;
	}
	 
	@RequestMapping(value = "/add")
	public ModelAndView add(HttpSession session, @RequestParam(value = "professionId", required = false) String professionId) {
		ModelAndView mv = new ModelAndView("subjectManage/subject_add");
		mv.getModelMap().put("professionId", StringUtils.isBlank(professionId) ? StringUtils.EMPTY : professionId);
		return mv;
	}
	
	@RequestMapping("/edit")
	public ModelAndView edit(Long id) {
		ModelAndView mv = new ModelAndView("subjectManage/subject_edit");
		if (null != id) {
//			User user = this.userService.get(id);
			Subject subject = this.subjectService.getCompleteSubject(id);
			mv.getModelMap().put("subject", subject);
		}
		return mv;
	}
	/**
	 * 查看二维码
	 * @param id
	 * @return
	 */
	@RequestMapping("/code")
	public ModelAndView code(Long id) {
		ModelAndView mv = new ModelAndView("subjectManage/view_code");
		if (null != id) {
//			User user = this.userService.get(id);
			Subject subject = this.subjectService.getCompleteSubject(id);
			mv.getModelMap().put("subject", subject);
		}
		return mv;
	}
	
	
	@ResponseBody
	@RequestMapping("/save")
	public Map<String, Object> save(Subject newSubject) {
		Map<String, Object> map = new HashMap<String, Object>();
		boolean result = false;
		String msg = null;
		Subject targetSubject = this.subjectService.getByName(newSubject.getName());
		// 新增
		if (null == targetSubject) {
			this.subjectService.saveOrUpdate(newSubject);
			result = true;
			msg = "新增成功";
		} else {
		// 更新
			this.subjectService.saveOrUpdate(newSubject);
			result = true;
			msg = "更新成功";
		}
		map.put("result", result);
		map.put("msg", msg);
		return map;
	}
	
	@ResponseBody
	@RequestMapping("/delete")
	public Result delete(Long id) {
		try {
			this.subjectService.delete(id);
			return new Result(true, "删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, "删除失败");
		}
	}
	
	@ResponseBody
	@RequestMapping("/subjects")
	public List<Map<String,Object>> subjects(Subject subject) {
		/*if (null != profession && null != profession.getId()) {
			org.setParentId(org.getId());
			org.setId(null);
		}*/
//		List<Subject> subjects = this.subjectService.list(subject);
		List<Map<String, Object>> list = this.subjectService.bulidTree(subject);
		return list;
	}
	
	@ResponseBody
	@RequestMapping("/assignSubject")
	public Result AssignSubject(
			@RequestParam(value = "id") Long id,
			@RequestParam(value = "subjectIds", required = false) String subjectIds) {
		try {
			this.subjectService.saveUserGroupSubject(id, subjectIds);
			System.out.println("id==" + id + "subjectIds==" + subjectIds);
			return Result.SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return Result.FAILURE;
		}
	}
}
